package net.linlan.utils.http;

import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 
 * Filename:HttpContextUtils.java
 * Desc: 获取应用和工程上下文的request对象
 *
 * @author Linlan
 * CreateTime:2018/4/22 12:58
 *
 * @version 1.0
 * @since 1.0
 *
 */
public class HttpContextUtils {

    /**
     * www主域
     */
    public static final String WWW = "www";

    /**
     * http前缀
     */
    public static final String HTTP = "http";

    /**
     * https前缀
     */
    public static final String HTTPS = "https";

    /**
     * http请求
     */
    public static final String URL_HTTP = HTTP + "://";

    /**
     * https请求
     */
    private static final String URL_HTTPS = HTTPS + "://";

    /** get http servlet request
     * @return  HttpServletRequest
     */
    public static HttpServletRequest getHttpServletRequest() {
        return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
    }

    /** get the source domain
     * @return  域名
     */
    public static String getDomain(){
        HttpServletRequest request = getHttpServletRequest();
        StringBuffer url = request.getRequestURL();
        return url.delete(url.length() - request.getRequestURI().length(), url.length()).toString();
    }

    /** get the source origin
     * @return  来源Origin
     */
    public static String getOrigin(){
        HttpServletRequest request = getHttpServletRequest();
        return request.getHeader("Origin");
    }

    /** get http servlet response
     * @return  响应体
     */
    public static HttpServletResponse getHttpServletResponse() {
        return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();
    }

}
